#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<conio.h>
struct telephone{
 	char name[20];
 	char phonenumber[20];
 	telephone *next;//定义一个指向结构类型的指针
}*head,//链表头
*current,*ptr;//current表示当前链表，ptr是当前链表的前一个链表
void enter();
void del();
void list();
void search();
void save();
void load();

int main(){
	int mode;
	do{
		printf("Choose your mode:\n\n");
		printf("0-退出通讯录\n");
		printf("1-通讯录录入\n");
		printf("2-通讯录删除\n");
		printf("3-通讯录显示\n");
		printf("4-通讯录查询\n");
		printf("5-通讯录保存\n");
		printf("6-通讯录加载\n");
		scanf("%d",&mode);
		system("cls");
		switch(mode){
			case 0:printf("已退出");
				break;
			case 1:enter();
				break;
			case 2:del();
				break;
			case 3:list();
				break;
			case 5:save();
				break;
			case 6:load();
		}
	}while(mode != 0);
 	return 0;
 }
 
 void enter(){
 	fflush(stdin);
	char input[20];
	//输入联系人
		printf("请输入联系人姓名：\n");
//		scanf("%s",&input);
		while (gets(input) != NULL&&input[0] != '\0'){
			current = (telephone*)malloc(sizeof(telephone));
			if (head == NULL){
			 	head = current;
			}
			else{
				ptr->next = current;
			}
			current->next = NULL;
			
 			strcpy(current->name,input);
			printf("请输入电话号码：\n");
			scanf("%s", current->phonenumber);
			printf("请输入下一个联系人（加1开始输入，输入空格结束）：\n");
			
			while (getchar() != '\n'){
			 	continue;
			}
			ptr = current;
			};
		system("cls");
		printf("已结束\n");
		
 }
 
 void del(){
 	list();
 	current = head;
 	int choise;
	char input[20];
 	printf("按姓名删除，输入1，按电话号码输入，输入0:");
 	scanf("%d",&choise);
 	if(choise){
 		printf("输入名称：\n");
 		scanf("%s",&input);
 		while(current->next != NULL&&strcmp(current->name,input) != 0){//循环查找要删除的节点
 			ptr = current;
 			current = current->next;
		};
		if(strcmp(current->name,input) == 0){
			printf("已删除%s",current->name);
 			if(strcmp(head->name,input) == 0){//找到的节点是头节点
 				head = current->next;
			}
			else{//找到的节点是普通节点
				ptr->next = current->next;
			}
			free(current);

		}
		else{//没有找到要删除的节点
			printf("未找到");
		}
	}
	else if(!choise){
		printf("输入号码：\n");
 		scanf("%s",&input);
 		while(current->next != NULL&&strcmp(current->phonenumber,input) != 0){//循环查找要删除的节点
 			ptr = current;
 			current = current->next;
		};
		if(strcmp(current->phonenumber,input) == 0){
			printf("已删除%s",current->name);
 			if(strcmp(head->phonenumber,input) == 0){//找到的节点是头节点
 				head = current->next;
			}
			else{//找到的节点是普通节点
				ptr->next = current->next;
			}
			free(current);

		}
		else{//没有找到要删除的节点
			printf("未找到");
		}
	}
	else{
		printf("输入错误！");
	}
	
 	
 }
 
 void list(){
 	current = head;
	while (current != NULL){
		printf("%s:%s\n",current->name,current->phonenumber);
		current = current->next;
	};
	printf("显示完成\n");
 }
 
 void save(){
 	current = head;
	FILE *fp=fopen("book.txt","w");
	while (current != NULL){
		fprintf(fp,"%s %s\n",current->name,current->phonenumber);
		current = current->next;
	}
//	清除指针current的内存
	current = head;
	while (current != NULL){
	 	free(current);
		current = current->next;
	}
	fclose(fp);
	system("cls");
	printf("已保存\n");
 }
 
 void load(){
 	fflush(stdin);
	FILE *fp=fopen("book.txt","r");
//	current = (telephone*)malloc(sizeof(telephone));
//	fscanf(fp,"%s %s",current->name,current->phonenumber);
//	head = current;
	while(1){
		current = (telephone*)malloc(sizeof(telephone));
		if (head == NULL){
			 	head = current;
		}
		else{
		ptr->next = current;
		}
		current->next = NULL;
		if(EOF == fscanf(fp,"%s %s",current->name,current->phonenumber)){
			ptr->next = NULL;
			free(current);
			break;
		}
//		printf("%s %s\n",current->name,current->phonenumber);
		ptr = current;
		current = current->next;
	};
	fclose(fp);
	system("cls");
	printf("读取完成\n");
 }